package com.ruoyi.web.controller.facilities;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.EquipmentSafetyProtective;
import com.ruoyi.system.service.IEquipmentSafetyProtectiveService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * createBy:yanzhengao
 * 2022/11/14
 * 设备安全防护设施 控制层
 */
@RestController
@RequestMapping("/facilities")
public class EquipmentSafetyProtectiveController {
    @Autowired
    private IEquipmentSafetyProtectiveService iEquipmentSafetyProtectiveService;

    // @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/equipment/safety/protective/list")
    public TableDataInfo list(EquipmentSafetyProtective request) {
        startPage();
        List<EquipmentSafetyProtective> list = iEquipmentSafetyProtectiveService.selectEquipmentSafetyProtectiveList(request);
        return getDataTable(list);
    }

    // @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "设备安全防护设施", businessType = BusinessType.INSERT)
    @PostMapping("/equipment/safety/protective/add")
    @PreAuthorize("@ss.hasPermi('facilities:equipment:safety:protective:add')")
    public AjaxResult add(@Validated @RequestBody EquipmentSafetyProtective request) {
        return toAjax(iEquipmentSafetyProtectiveService.saveEquipmentSafetyProtective(request));
    }

    // @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "设备安全防护设施", businessType = BusinessType.UPDATE)
    @PutMapping("/equipment/safety/protective/update")
    @PreAuthorize("@ss.hasPermi('facilities:equipment:safety:protective:update')")
    public AjaxResult edit(@Validated @RequestBody EquipmentSafetyProtective request) {
        if (null == request) {
            return AjaxResult.error("参数异常!");
        }
        if (null == request.getId()) {
            return AjaxResult.error("id不可为空!");
        }
        return toAjax(iEquipmentSafetyProtectiveService.editEquipmentSafetyProtective(request));
    }

    // @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "设备安全防护设施", businessType = BusinessType.DELETE)
    @DeleteMapping("/equipment/safety/protective/delete/{ids}")
    @PreAuthorize("@ss.hasPermi('facilities:equipment:safety:protective:delete')")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (CollectionUtils.isEmpty(Arrays.asList(ids))) {
            return AjaxResult.error("id不可为空!");
        }
        iEquipmentSafetyProtectiveService.deleteEquipmentSafetyProtective(ids);
        return success();
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageUtils.startPage();
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    protected AjaxResult toAjax(int rows) {
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    public AjaxResult success() {
        return AjaxResult.success();
    }

    public static AjaxResult success(Object data)
    {
        return AjaxResult.success("操作成功", data);
    }

    @Log(title = "作业场地防护设施(", businessType = BusinessType.IMPORT)
    @PostMapping("/equipment/safety/protective/importData")
    @PreAuthorize("@ss.hasPermi('facilities:equipment:safety:protective:importData')")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<EquipmentSafetyProtective> util = new ExcelUtil<>(EquipmentSafetyProtective.class);
        List<EquipmentSafetyProtective> equipmentSafetyProtectives = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = iEquipmentSafetyProtectiveService.importExcel(equipmentSafetyProtectives, updateSupport, operName);
        return success(message);
    }

    @Log(title = "作业场地防护设施(", businessType = BusinessType.EXPORT)
    @PostMapping("/equipment/safety/protective/export")
    @PreAuthorize("@ss.hasPermi('facilities:equipment:safety:protective:export')")
    public void export(HttpServletResponse response, EquipmentSafetyProtective equipmentSafetyProtective) {
        List<EquipmentSafetyProtective> list = iEquipmentSafetyProtectiveService.selectEquipmentSafetyProtectiveList(equipmentSafetyProtective);
        ExcelUtil<EquipmentSafetyProtective> util = new ExcelUtil<>(EquipmentSafetyProtective.class);
        util.exportExcel(response, list, "作业场地防护设施(");
    }

    @PostMapping("/equipment/safety/protective/importTemplate")
    @PreAuthorize("@ss.hasPermi('facilities:equipment:safety:protective:importTemplate')")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<EquipmentSafetyProtective> util = new ExcelUtil<>(EquipmentSafetyProtective.class);
        util.importTemplateExcel(response, "设备安全防护设施");
    }

    public String getUsername() {
        return "";
    }


}
